package ModelLayer;
import java.util.ArrayList;


/**
 * A singleton class to hold a list of corporate customers
 * 
 * @author Mike M. Madsen, Rasmus A. Borup
 * @version 15/12-2013
 */
public class CorporateCustomerContainer
{
    // instance variables
    private ArrayList<CorporateCustomer> corporateCustomers;
    private static CorporateCustomerContainer instance = null;
    
    /**
     * Constructor for objects of class CorporateCustomerContainer
     * When called adds 4 dummy customers to the list for testing
     * Singleton
     */
    private CorporateCustomerContainer()
    {
        corporateCustomers = new ArrayList<CorporateCustomer>();
        CorporateCustomer corpCus1 = new CorporateCustomer("Rasmus Teknik", "1@corpCus.dk", "Rasmus Vej 1", "12345678", "9000", "Aalborg", "1234567890", true);
        CorporateCustomer corpCus2 = new CorporateCustomer("Anders Tømmerfirma", "2@corpCus.dk", "Anders Vej 2", "22345678", "9200", "Aalborg", "2234567890", true);
        CorporateCustomer corpCus3 = new CorporateCustomer("Mikes Murerbiks", "3@corpCus.dk", "Mike Vej 3", "32345678", "9300", "Aalborg", "3234567890", true);
        CorporateCustomer corpCus4 = new CorporateCustomer("Vestbjerg Feriecenter", "4@corpCus.dk", "Tilfædigvej 4", "42345678", "9400", "Aalborg", "4234567890", false);        
        corporateCustomers.add(corpCus1);
        corporateCustomers.add(corpCus2);
        corporateCustomers.add(corpCus3);
        corporateCustomers.add(corpCus4);
    }
    
    /**
     * Returns an an instance of the class. If one allready exists it 
     * returns that on else creates a new one
     */
    public static CorporateCustomerContainer getInstance()
    {
        if(instance == null)
        {
            instance = new CorporateCustomerContainer();
        }
        return instance;
    }
    
    /**
     * Adds a customer to the list
     */
    public void addCustomer(CorporateCustomer customer)
    {
        corporateCustomers.add(customer);
    }
    
    /**
     * Removes a customer from the list determined by id
     */
    public void removeCustomer(String id)
    {
        if(getCorporateCustomerByID(id) != null)
        {
            corporateCustomers.remove(getCorporateCustomerByID(id));
        }
    }
    
    /**
     * Finds and returns a customer in the list found by id
     */
    public CorporateCustomer getCorporateCustomerByID(String id)
    {
        boolean found = false;
        int i = 0;
        CorporateCustomer customer = null;
        
        while(i < corporateCustomers.size() && !found)
        {
            if(corporateCustomers.get(i).getID().equals(id))
            {
                customer = corporateCustomers.get(i);
                found = true;
            }
            else
            {
                i++;
            }
        }
        if(customer != null)
        {
            return customer;
        }
        else
        {
            return null;
        }
    }
    
    /**
     * Finds and returns a customer in the list found by phonr number
     */
    public CorporateCustomer getCorporateCustomerByPhone(String phoneNO)
    {
        boolean found = false;
        int i = 0;
        CorporateCustomer customer = null;
        
        while(i < corporateCustomers.size() && !found)
        {
            if(corporateCustomers.get(i).getPhoneNO().equals(phoneNO))
            {
                customer = corporateCustomers.get(i);
                found = true;
            }
            else
            {
                i++;
            }
        }
        if(customer != null)
        {
            return customer;
        }
        else
        {
            return null;
        }
    }
}